using System.Diagnostics;

namespace Bread.Utility;

public class ProcessHelper
{
    public static void ExeCmd(string target, params string[] paras)
    {
        string cmds = string.Empty;
        foreach (var s in paras) {
            cmds += s;
            cmds += " ";
        }
        cmds = cmds.TrimEnd(' ', '&');

        using Process p = new Process();

        p.StartInfo.FileName = target;
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.CreateNoWindow = true;
        p.StartInfo.Arguments = cmds;

        p.Start();
        p.WaitForExit();
    }

    public static async Task ExeCmdAsync(string fileName, string arguments)
    {
        var psi = new ProcessStartInfo {
            FileName = fileName,
            Arguments = arguments,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            UseShellExecute = false,
            CreateNoWindow = true
        };

        using var process = new Process { StartInfo = psi };
        process.EnableRaisingEvents = true;
        process.OutputDataReceived += (s, ev) => Console.WriteLine(ev.Data);
        process.ErrorDataReceived += (s, ev) => Console.WriteLine(ev.Data);

        process.Start();
        process.BeginOutputReadLine();
        process.BeginErrorReadLine();
        await process.WaitForExitAsync();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="target"></param>
    /// <param name="paras"></param>
    public static Process AsyncExeCmd(string target, params string[] paras)
    {
        string cmds = string.Empty;
        foreach (var s in paras) {
            cmds += s;
            cmds += " ";
        }
        cmds = cmds.TrimEnd(' ', '&');

        Process p = new Process();
        p.StartInfo.FileName = target;
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.CreateNoWindow = false;
        p.StartInfo.Arguments = cmds;
        p.Start();
        return p;
    }


    public static string ShellCmd(params string[] paras)
    {
        string output = string.Empty;
        string cmds = string.Empty;
        foreach (var s in paras) {
            cmds += s;
            cmds += " ";
        }
        cmds = cmds.TrimEnd(' ', '&');
        cmds += " &exit"; //防止假死

        using (Process p = new Process()) {
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.Start();
            p.StandardInput.WriteLine(cmds);
            p.StandardInput.AutoFlush = true;
            output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            p.Close();
        }
        return output;
    }

   


    public static string? GetExePath()
    {
        var path = Environment.ProcessPath;
        if (string.IsNullOrWhiteSpace(path)) {
            path = System.AppDomain.CurrentDomain.BaseDirectory;
        }
        if (string.IsNullOrWhiteSpace(path)) return null;
        var ext = Path.GetExtension(path) ?? "";
        if (ext.ToLower() == ".exe") {
            return path;
        }
        var name = Path.GetFileNameWithoutExtension(path);
        if (string.IsNullOrWhiteSpace(name)) return string.Empty;
        var dir = Path.GetDirectoryName(path);
        if (string.IsNullOrWhiteSpace(dir)) return null;
        return Path.Combine(dir, $"{name}.exe");
    }
}
